package com.custom.base.view

import android.content.Context
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.util.AttributeSet
import android.view.KeyEvent
import android.view.View
import android.widget.EditText
import android.widget.FrameLayout
import android.widget.LinearLayout
import com.custom.base.R
import com.custom.base.util.SDViewUtil

class PhoneCodeView(context: Context, attrs: AttributeSet) : LinearLayout(context, attrs) {
    private var listener: OnCodeListener? = null
    private var phoneCode = 6
    private var etWidth = 0
    private var viewWidth = 0
    private var viewSelectColor = 0
    private var viewNoSelectColor = 0
    private var requestFocus = false
    private var ets:ArrayList<EditText> = arrayListOf()
    private var vs:ArrayList<View> = arrayListOf()
    init {
        val attributes = context.obtainStyledAttributes(attrs, R.styleable.PhoneCodeView)
        attributes.apply {
            phoneCode = getInt(R.styleable.PhoneCodeView_phoneCode_length, 6)
            etWidth = getDimensionPixelSize(R.styleable.PhoneCodeView_phoneCode_etWidth, 0)
            viewWidth = getDimensionPixelSize(R.styleable.PhoneCodeView_phoneCode_viewWidth, 0)
            viewSelectColor = getColor(R.styleable.PhoneCodeView_phoneCode_viewSelectColor, Color.parseColor("#FF000000"))
            viewNoSelectColor = getColor(R.styleable.PhoneCodeView_phoneCode_viewNoSelectColor, Color.parseColor("#C6C6C6FF"))
            requestFocus = getBoolean(R.styleable.PhoneCodeView_phoneCode_requestFocus, false)
        }

        for (i in 0 until phoneCode) {
            val view = SDViewUtil.getRId(context, R.layout.view_phonecode)
            val ll = view.findViewById<FrameLayout>(R.id.view_phonecode_ll)
            val et = view.findViewById<EditText>(R.id.view_phonecode_Et)

            if (etWidth != 0) ll.layoutParams.width = etWidth

            et.addTextChangedListener(object : TextWatcher {
                override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
                override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
                override fun afterTextChanged(s: Editable) {
                    if (s.isNotEmpty()) {
                        if(i == ets.size-1){
                            var code = ""
                            for (e in ets) {
                                code += e.text.toString()
                            }
                            SDViewUtil.hideInputMethod(et)
                            listener?.onCode(code)
                        }else{
                            SDViewUtil.showInputMethod(ets[i+1])
                        }
                    }
                }
            })
            et.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) {
                    for (a in vs.indices) {
                        if(a == i){
                            vs[a].setBackgroundColor(viewSelectColor)
                        } else {
                            vs[a].setBackgroundColor(viewNoSelectColor)
                        }
                    }
                }
            }
            et.setOnKeyListener { _, keyCode, event ->
                if(keyCode == KeyEvent.KEYCODE_DEL && event.action == KeyEvent.ACTION_DOWN && et.text.isEmpty()){
                    if(i > 0){
                        SDViewUtil.showInputMethod(ets[i-1])
                    }
                    et.setText("")
                    return@setOnKeyListener true
                }
                return@setOnKeyListener false
            }

            ets.add(et)
            view.layoutParams = LayoutParams(0, LayoutParams.MATCH_PARENT, 1f)
            addView(view)

            if(i == 0&&requestFocus) SDViewUtil.showInputMethod(et)
        }
    }

    interface OnCodeListener{
        fun onCode(code:String)
    }

    fun setOnCodeListener(listener: OnCodeListener){
        this.listener = listener
    }
}